package com.runchamp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.runchamp.util.DistanceCircleView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* This dialog allows the user to create new races*/
public class QuickRaceMenu extends Activity implements View.OnClickListener {

    public de.hdodenhof.circleimageview.CircleImageView circle1, circle2, circle4, circle5, circle8, circle10;
    private Intent raceInfoIntent;
    private AsyncHttpClient client;
    private RequestParams params;
    private String USER;
    private String distance;
    private SharedPreferences prefs;
    private JSONAdapterRacesList mJSONAdapter;
    private String SERVER_URL = "http://runtobo.appspot.com/quickrace/addtorace";
    private TextView distanceKM1, distanceKM2, distanceKM4, distanceKM5, distanceKM8, distanceKM10;
    private TextView distanceKM_ITSELF_1, distanceKM_ITSELF_2, distanceKM_ITSELF_4, distanceKM_ITSELF_5, distanceKM_ITSELF_8, distanceKM_ITSELF_10;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /*initialize all the "special" circles representing the distance*/
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_race_menu);
        prefs = getSharedPreferences("MyPrefsFile", Context.MODE_PRIVATE);
        USER = prefs.getString("userID", "");
        raceInfoIntent = new Intent(this, RaceInfo.class);
        client = new AsyncHttpClient();
        params = new RequestParams();
        params.put("userID", USER);
        circle1 = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.km1);
        distanceKM1 = (TextView) findViewById(R.id.text_km1);
        distanceKM_ITSELF_1 = (TextView) findViewById(R.id.text_km_itslef_1);
        DistanceCircleView.setCircle("0.2", circle1, distanceKM1, distanceKM_ITSELF_1);

        circle2 = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.km2);
        distanceKM2 = (TextView) findViewById(R.id.text_km2);
        distanceKM_ITSELF_2 = (TextView) findViewById(R.id.text_km_itslef_2);
        DistanceCircleView.setCircle("2", circle2, distanceKM2, distanceKM_ITSELF_2);

        circle4 = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.km4);
        distanceKM4 = (TextView) findViewById(R.id.text_km4);
        distanceKM_ITSELF_4 = (TextView) findViewById(R.id.text_km_itslef_4);
        DistanceCircleView.setCircle("4", circle4, distanceKM4, distanceKM_ITSELF_4);

        circle5 = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.km5);
        distanceKM5 = (TextView) findViewById(R.id.text_km5);
        distanceKM_ITSELF_5 = (TextView) findViewById(R.id.text_km_itslef_5);
        DistanceCircleView.setCircle("5", circle5, distanceKM5, distanceKM_ITSELF_5);

        circle8 = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.km8);
        distanceKM8 = (TextView) findViewById(R.id.text_km8);
        distanceKM_ITSELF_8 = (TextView) findViewById(R.id.text_km_itslef_8);
        DistanceCircleView.setCircle("8", circle8, distanceKM8, distanceKM_ITSELF_8);

        circle10 = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.km10);
        distanceKM10 = (TextView) findViewById(R.id.text_km10);
        distanceKM_ITSELF_10 = (TextView) findViewById(R.id.text_km_itslef_10);
        DistanceCircleView.setCircle("10", circle10, distanceKM10, distanceKM_ITSELF_10);
        this.setTitle("Choose distance");
        circle1.setOnClickListener(this);
        circle2.setOnClickListener(this);
        circle4.setOnClickListener(this);
        circle5.setOnClickListener(this);
        circle8.setOnClickListener(this);
        circle10.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_quick_race_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        if (v == circle1) {
            distance = "0.2";
        } else if (v == circle2) {
            distance = "2";
        } else if (v == circle4) {
            distance = "4";
        } else if (v == circle5) {
            distance = "5";
        } else if (v == circle8) {
            distance = "8";

        } else {
            distance = "10";
        }
        /*finds relevant race
        *adds user to race
        *gets all race data and put it to intent
        *starts raceInfo intent*/

        params.put("distance", distance);
        client.post(SERVER_URL, params, new JsonHttpResponseHandler() {


            @Override
            public void onSuccess(JSONArray response) {
                try {
                    JSONObject race = response.getJSONObject(0);
                    String Key = race.get("raceID").toString();
                    if (!Key.equals("-100")) {
                        String Name = race.get("Manager").toString();
                        String RaceDistance = race.get("Distance").toString();
                        String Time = race.get("Time").toString();
                        String Manager = race.get("Manager").toString();
                        String ManagerId = race.get("ManagerId").toString();
                        String Participants = race.get("Participants").toString();
                        String Tags = race.get("Tags").toString();
                        String Estimated = race.get("Estimated").toString();
                        Bundle raceBundle = new Bundle();
                        raceBundle.putString("Key", Key);
                        raceBundle.putString("Name", Name);
                        raceBundle.putString("Distance", RaceDistance);
                        raceBundle.putString("Time", Time);
                        raceBundle.putString("Manager", Manager);
                        raceBundle.putString("ManagerId", ManagerId);
                        raceBundle.putString("Participants", Participants);
                        raceBundle.putString("Tags", Tags);
                        raceBundle.putString("Estimated", Estimated);
                        raceInfoIntent.putExtras(raceBundle);
                        startActivity(raceInfoIntent);
                        finish();
                    }
                    //no race found, server is not available
                    else {
                        Toast.makeText(getApplicationContext(), "Sorry, race is not available", Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable e, JSONObject errorResponse) {
                Log.i("quick server failed", "!");
            }
        });

    }
}
